package testcase.app;

import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import api.app.ChangePassword;
import api.app.VerificationCodeForPassword;
import api.app.VerificationPhoneCode;
import cn.bmyi.sms.Sms;
import common.Host;
import common.TestData;

/**
 * 帮忙医密码修改ChangePassword接口测试用例
 * @author lipeng
 *
 */
public class TestChangePassword{
	/**
	 * 帮忙医密码修改ChangePassword接口测试环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	 @DataProvider (name = "Test")
          public static String[][] TestChangePasswordDataProvider() throws Exception{
		  Host.setup("Test");
		  TestData td = new TestData("app/TestChangePassword.xml");
		  return td.getParam();
	 }
	 
	/**
	 * 帮忙医修改密码ChangePassword接口
	 * @param caseName       -用例名称
	 * @param Telephone      -电话号码
	 * @param Password       -密码
	 * @throws Exception
	 */
	 @Test (dataProvider = "Test" , groups = {"Test"})
	 public void testChangePasswordTestCase(String caseName,String telephone,String password)throws Exception{
		 
		 Host.setup("test");
			 
		 if(!caseName.equals("noVarificationCode"))
		 {
			 String getCodeResult = VerificationCodeForPassword.post(telephone);
			 
			 Boolean isMobileExist = new JSONObject(getCodeResult).getJSONObject("Data").getBoolean("MobileExist");
			 if (!isMobileExist) //判断验证码是否获取成功，如果没有获取成功，测试用例执行失败
			 {
				 Assert.fail("手机号未注册，获取验证码失败");
			 }
			 
		     String smsCode=Sms.getVerificationCodeRepeatedly(telephone);
		     String VeriCodeResult = VerificationPhoneCode.post(telephone,smsCode);
		     VeriCodeResult = new JSONObject(VeriCodeResult).getJSONObject("Data").getString("verificationstatus");
		     
		     if (!VeriCodeResult.equals("Success")) //判断验证码是否校验成功，如果没有校验成功，测试用例执行失败
		     {
		    	 Assert.fail("校验验证码失败");
		     }	     
		 }
		 
		 //调用接口并将接口返回数据转换成json格式 
		 String result = ChangePassword.post(telephone, password);
		 JSONObject jsonResult;
		 if(result == null || result.isEmpty())
		 {
			 jsonResult = null;
		 }
		 else
		 {
			 jsonResult = new JSONObject(result);
		 }

		switch (caseName)
		{	
			case "changePassrwordSuccess":
				Assert.assertEquals(jsonResult.getString("Message"), "修改密码成功");
				break;
			case "noPassword":
				Assert.assertTrue(result.isEmpty());
				break;
			case "noVarificationCode":
				Assert.assertEquals(jsonResult.getString("Message"), "修改密码失败");
				break;
			default:
				Assert.fail();
				break;
		}		
	 }
}